package com.opgea.crm.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.common.SetupsAttributeQualifier;
import com.opgea.crm.common.SetupsModuleQualifier;
import com.opgea.crm.common.vo.DatePeriodVo;
import com.opgea.crm.common.vo.OpportunityVo;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.OpportunityService;
import com.opgea.crm.services.QualifierService;
import com.opgea.crm.services.SetupService;

@Controller
@RequestMapping("opportunityreports")
public class OpportunityReportController {
	
	@Autowired
	OpportunityService opportunityService;
	@Autowired
	QualifierService qualifierService; 
	@Autowired
	SetupService setupService; 
	
	@RequestMapping(method=RequestMethod.GET)
	public String show(HttpServletRequest request){
		return "OpportunityReport";
	}
	
	
	@RequestMapping(method=RequestMethod.GET, value="showPeriodDate")
	public@ResponseBody
	Map<String,Object> showPeriodDate(@RequestParam(required=false, value="periodCriteria")String periodCriteria){
		DatePeriodVo periodVo = opportunityService.findPeriod(periodCriteria);
		return JsonModelMap.success().data(periodVo);
	}
	
	@RequestMapping(method=RequestMethod.GET ,value="showOpportunityDynamically")
	public @ResponseBody
	Map<String, Object> showDynamically(
			@RequestParam(required=false, value="periodFrom")String periodFrom,
			@RequestParam(required=false, value="periodTo")String periodTo,
			@RequestParam(required=false, value="statusType")String statusType,
			@RequestParam(required=false, value="stageType")String stageType
			){
		if(periodFrom != null || periodTo != null){
			String periodFromDate = periodFrom.substring(0, periodFrom.indexOf('T'));
			String periodToDate = periodTo.substring(0, periodTo.indexOf('T'));

			List<OpportunityVo> opportunities = opportunityService.findReport(periodFromDate, periodToDate, statusType, stageType);
			return JsonModelMap.success().data(opportunities);			
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "searchPeriodTypes")
	public @ResponseBody
	Map<String, Object> searchPriodTypes() {
		return JsonModelMap.success().data(qualifierService.getSearchPeriodQualifiers());
	}	
	
	
	@RequestMapping(method=RequestMethod.GET, value="statusTypes")
    public @ResponseBody Map<String, Object> statusTypes(){
		List<SetupsValue> statusList = setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(),SetupsAttributeQualifier.Status.ordinal());
		statusList.add(new SetupsValue(0l, "All"));
		return JsonModelMap.success().data(statusList);
	   //return JsonModelMap.success().data(setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(),SetupsAttributeQualifier.Status.ordinal()));
   }
	
	 @RequestMapping(method=RequestMethod.GET, value="stageTypes")
	 public @ResponseBody Map<String, Object> stageTypes()
	 {
		 List<SetupsValue> stageList = setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(),SetupsAttributeQualifier.Stage.ordinal());
		 stageList.add(new SetupsValue(0l, "All"));
		 return JsonModelMap.success().data(stageList);
	 	 //return JsonModelMap.success().data(setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(),SetupsAttributeQualifier.Stage.ordinal()));
	 }
	
	

}
